package com.itheima.job;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.AliFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @PackageName: com.itheima.job
 * @ClassName: DeletePicJob
 * @Author: Solomon
 * @Date: 2020/7/6 18:51
 * @Description:
 */
public class DeletePicJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){
        //调用redis中set集合的方法，求取两个集合的差集，里面是图片的名称集合
        Set<String> imgSet = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES,
                RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        //如果差集不为空
        if (imgSet != null){
            //遍历集合
            for (String picName : imgSet) {
                //从阿里云中删除
                AliFileUtil.deleteFile(picName);
                //从redis中删除
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
                System.out.println("自定义任务执行，清理垃圾图片" + picName);
            }
        }
    }
}
